package com.shelpe.services.lexicon.service.filter;

import java.util.ArrayList;
import java.util.List;

import com.shelpe.services.lexicon.constants.Device;
import com.shelpe.services.lexicon.entity.AdgroupRecord;
import com.shelpe.services.lexicon.entity.KeywordRecommend;
import com.shelpe.services.lexicon.service.filter.support.KeywordRecommendFilter;

import lombok.Setter;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Setter
public class IntersectionKeywordRecommendsFilter implements KeywordRecommendsFilter {

	private List<KeywordRecommendFilter> filters;
	
	@Override
	public List<KeywordRecommend> run(List<KeywordRecommend> recommends, AdgroupRecord record, Device device) {
		List<KeywordRecommend> remainings = new ArrayList<KeywordRecommend>();
		for(KeywordRecommend recommend: recommends){
			boolean supported = true;
			
			if(this.filters != null && recommend.getInsight() != null){
				for(KeywordRecommendFilter filter: this.filters){
					KeywordRecommend ret = filter.run(recommend, record, device);
					supported = supported && (ret != null);
					if(!supported){
						break;
					}
				}
			}
			
			if(supported){
				remainings.add(recommend);
			}else{
				log.debug("{} is removed by filter", recommend.getWord());
			}
		}
		return remainings;
	}
	
	public void addFilter(KeywordRecommendFilter filter){
		if(this.filters == null){
			this.filters = new ArrayList<KeywordRecommendFilter>();
		}
		this.filters.add(filter);
	}

}
